package com.guet.controller.user;

import com.guet.DTO.CommentAddDTO;
import com.guet.DTO.CommentDTO;
import com.guet.DTO.TopicSaveDTO;
import com.guet.VO.DeleteTopicAndReplyVO;
import com.guet.VO.TemplateTopicVO;
import com.guet.VO.TopicVO;
import com.guet.service.TopicService;
import com.guet.result.Result;
import com.guet.result.UnfinityPageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("studentTopicController")
@RequestMapping("student/topic")
@Slf4j
@CrossOrigin
public class TopicController {
    @Autowired
    private TopicService topicService;

    /**
     * 按照类型获取话题列表
     *
     * @param type
     * @return
     */
    @GetMapping("{type}")
    public Result<List<TopicVO>> getTopicList(@PathVariable String type) {
        List<TopicVO> result = topicService.list(type);
        return Result.success(result);
    }

    /**
     * 获取话题内容
     *
     * @param id
     * @return
     */
    @GetMapping
    public Result<TopicVO> getTopicDetail(String id) {
        TopicVO topicVO = topicService.getTopicDetail(id);
        return Result.success(topicVO);
    }

    /**
     * 获取话题下的评论
     *
     * @return
     */
    @GetMapping("comment")
    public Result<UnfinityPageResult> getCommentList(CommentDTO commentDTO) {
        UnfinityPageResult list = topicService.getCommentList(commentDTO);
        return Result.success(list);
    }

    /**
     * 增加评论
     */
    @PutMapping
    public Result<Object> addComment(@RequestBody CommentAddDTO commentAddDTO) {
        log.info("add comment {}", commentAddDTO);
        topicService.addComment(commentAddDTO);
        return Result.success();
    }

    /**
     * 保存话题草稿
     *
     * @param topicSaveDTO
     * @return
     */
    @PostMapping("draft")
    public Result<Object> saveTopicEdit(@RequestBody TopicSaveDTO topicSaveDTO) {
        log.info("save topic {}", topicSaveDTO);
        topicService.save(topicSaveDTO);
        return Result.success();
    }

    /**
     * 获取话题草稿
     *
     * @return
     */
    @GetMapping("draft")
    public Result<Object> getTopicEdit() {
        TemplateTopicVO templateTopicVO = topicService.getDraft();
        return Result.success(templateTopicVO);
    }

    /**
     * 发布话题
     *
     * @param topicSaveDTO
     * @return
     */
    @PostMapping
    public Result<Object> publishTopic(@RequestBody TopicSaveDTO topicSaveDTO) {
        topicService.publish(topicSaveDTO);
        return Result.success();
    }

    @GetMapping("illegal")
    public Result<List<DeleteTopicAndReplyVO>> getIllegalTopicList() {
        List<DeleteTopicAndReplyVO> list = topicService.getIllegalTopicList();
        return Result.success(list);
    }
}
